/*
  (C) 2005 Adam D. Bradley

  This program is free software; you can redistribute it and/or
  modify it under the terms of Version 2 of the GNU General Public 
  License.
  
  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.
  
  You should have received a copy of the GNU Library General Public
  License along with this library; if not, write to the Free
  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

package step;

import java.net.URI;
import java.util.ArrayList;

import step.exceptions.STEPException;

public class SenderPostSocketNode extends SenderSocketNode
{
	public SenderPostSocketNode(String id, String targetID, URI targetURI) 
	{
		super(id, targetID, targetURI);
	}

	public SenderPostSocketNode(String targetID, URI targetURI) {
		super(targetID, targetURI);
	}

	public String GetInfoXML() { 
		return super.GetInfoXML("POST", "sender", TargetURI); 
	}
	
    http.Client poster;

	public boolean IsEnabled() {
		/* See Evaluate() for rationale... */
		if (poster!=null) {
			if (poster.Done())
				return true;
			else 
				return false;
		}
		if (HasFreshInput(0))
			return true;
		return false;
	}

	protected void Evaluate() throws EvaluationFailure {
		/* Only do a new transmit if an extant transmission has
		 * completed, otherwise we can get race conditions. */
		if (poster != null) {
			if (poster.Done()) {
				poster = null;
			}
			return;
		}
		if (HasFreshInput(0)) {
			value = GetInput(0);
			poster = http.Client.POST_nb(TargetURI, value);
			rt_evaluated = true;
		}
	}


    public void Write(java.io.OutputStream out, int depth) throws java.io.IOException
    {
	WriteBasis(out, depth, "type=\"sender\" method=\"post\"");
    }

	@Override
	public void validate(Graph g, ArrayList<STEPException> compilationErrors) {
		// TODO Auto-generated method stub
	}
	
	@Override
	public Node ShallowCopy() {
		SenderPostSocketNode cn = new SenderPostSocketNode(ID, TargetID, TargetURI);
		Node.CopyNonRTPropertiesFromAtoB(this,cn);
		return cn;
	}
}

